package com.topgay.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.topgay.dao.ChattingDAO;
import com.topgay.domain.ChattingMessage;

@Service
public class ChattingService {

	@Autowired
	private ChattingDAO dao;
	
	public ChattingMessage sendMessage(String fromId, String toId, String message){
		int rowNum = selectMessageCount()+1;
		dao.insertMessage(new ChattingMessage(rowNum, message, fromId, toId));
		
		return dao.selectMessageByNum(rowNum);
	}
	
	public int selectMessageCount(){
		List<ChattingMessage> tmpList= dao.selectAllMessage();
		int rowNum = 0;
		
		for(ChattingMessage tmp : tmpList){
			if(tmp.getCount() > rowNum) rowNum = tmp.getCount(); 
		}
		
		return rowNum;
	}
	
	public List<ChattingMessage> selectMessageById(String fromId){
		return dao.selectMessageById(fromId);
	}
	
	public ChattingMessage selectMessageByNum(int rowNum){
		return dao.selectMessageByNum(rowNum);
	}
	
}
